import {Get_Result} from "../axdr/Get_Result";
import {FrameData} from "../bean/FrameData";
import {DataUtils} from "../DataUtils";
import {resultCode} from "../tools/DataTool";
import {SubSeqOfResults} from "../axdr/GetResponseNormalList";
import {SubChoiceRespData} from "../axdr/A_ResultRecord";
import {SubSeqOfRecordResults} from "../axdr/GetResponseRecordList";
import {SubChoiceFrameResp} from "../axdr/GetResponseNext";
import {parseData} from "./ParseTools";


export function parseGetResponseNormal(get_result: Get_Result): String {
    let frameData = new FrameData();
    switch (get_result.getChoiceIndex()) {
        case 0:
            let dar = {
                dar: {},
            };
            get_result.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(get_result.dar.dataCode, 0, get_result.dar.dataCode.length);
            frameData.data = dar;
            break;
        case 1:
            let value = parseData(get_result.data);
            frameData.data = value;
            break;
    }
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseGetResponseNormalList(subSeqOfResults: SubSeqOfResults) {
    let frameData = new FrameData();
    let get_list_data = new Array<any>();
    let get_list = {
        seq_get_list: {

        },
    }
    for (let i = 0; i < subSeqOfResults.size(); i++) {
        switch (subSeqOfResults.get(i).result.getChoiceIndex()) {
            case 0:
                let dar = {
                    dar: {},
                };
                subSeqOfResults.get(i).result.dar.encodeAndSave(1000);
                dar.dar = DataUtils.bytesToHexString(subSeqOfResults.get(i).result.dar.dataCode, 0, subSeqOfResults.get(i).result.dar.dataCode.length);
                get_list_data.push(dar);
                break;
            case 1:
                let value = parseData(subSeqOfResults.get(i).result.data);
                get_list_data.push(value);
                break;
        }
    }
    get_list.seq_get_list = get_list_data;
    frameData.code = resultCode.success;
    frameData.data = get_list;
    return JSON.stringify(frameData);
}

export function parseGetResponseRecord(subChoice_response: SubChoiceRespData): String {
    let frameData = new FrameData();
    switch (subChoice_response.getChoiceIndex()) {
        case 0:
            let dar = {
                dar: {},
            };
            subChoice_response.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(subChoice_response.dar.dataCode, 0, subChoice_response.dar.dataCode.length);
            frameData.data = dar;
            break;
        case 1:
            let seq_of_a_record = {
                seq_of_a_record: {},
            };
            let a_record = {
                a_record: {},
            }
            let seq_a_record_list = new Array<any>();
            for (let i = 0; i < subChoice_response.records.size(); i++) {
                let seq_data = {
                    seq_of_data: {}
                }
                let seq_data_list = new Array<String>();
                for (let j = 0; j < subChoice_response.records.get(i).data.size(); j++) {
                    let value = parseData(subChoice_response.records.get(i).data.get(j));
                    seq_data_list.push(value);
                }
                a_record.a_record = seq_data_list
                seq_data.seq_of_data = seq_data_list;
                // seq_data.seq_of_data = seq_data_list;
                seq_a_record_list.push(a_record);
            }
            seq_of_a_record.seq_of_a_record = seq_a_record_list;
            frameData.data = seq_of_a_record;
            break;
    }
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseGetResponseRecordList(results: SubSeqOfRecordResults): String {
    let frameData = new FrameData();
    let get_record_data = new Array<any>();
    let get_record_list = {
        seq_get_record_list: {

        },
    }
    for (let i = 0; i < results.size(); i++) {
        switch (results.get(i).respData.getChoiceIndex()) {
            case 0:
                let dar = {
                    dar: {},
                };
                results.get(i).respData.dar.encodeAndSave(1000);
                dar.dar = DataUtils.bytesToHexString(results.get(i).respData.dar.dataCode, 0, results.get(i).respData.dar.dataCode.length);
                get_record_data.push(dar);
                break;
            case 1:
                let seq_of_a_record = {
                    seq_of_a_record: {},
                };
                let a_record = {
                    a_record: {},
                }
                let seq_a_record_list = new Array<any>();
                for (let j = 0; j < results.get(i).respData.records.size(); j++) {
                    let seq_data = {
                        seq_of_data: {}
                    }
                    let seq_data_list = new Array<String>();
                    for (let k = 0; k < results.get(i).respData.records.get(j).data.size(); k++) {
                        let value = parseData(results.get(i).respData.records.get(j).data.get(k));
                        seq_data_list.push(value);
                    }
                    a_record.a_record = seq_data_list
                    seq_data.seq_of_data = seq_data_list;
                    seq_a_record_list.push(a_record);
                }
                seq_of_a_record.seq_of_a_record = seq_a_record_list;
                get_record_data.push(seq_of_a_record);
                break;
        }
    }
    get_record_list.seq_get_record_list = get_record_data
    frameData.data = get_record_list;
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseGetResponseNext(subChoiceFrameResp: SubChoiceFrameResp): String {
    let frameData = new FrameData();
    switch (subChoiceFrameResp.getChoiceIndex()) {
        case 0:
            let dar = {
                dar: {},
            };
            subChoiceFrameResp.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(subChoiceFrameResp.dar.dataCode, 0, subChoiceFrameResp.dar.dataCode.length);
            frameData.code = resultCode.success;
            frameData.data = dar;
            break;
        case 1:
            let get_list_data = new Array<any>();
            let get_list = {
                seq_get_list: {

                },
            }
            for (let i = 0; i < subChoiceFrameResp.attr.size(); i++) {
                switch (subChoiceFrameResp.attr.get(i).result.getChoiceIndex()) {
                    case 0:
                        let dar = {
                            dar: {},
                        };
                        subChoiceFrameResp.attr.get(i).result.dar.encodeAndSave(1000);
                        dar.dar = DataUtils.bytesToHexString(subChoiceFrameResp.attr.get(i).result.dar.dataCode, 0, subChoiceFrameResp.attr.get(i).result.dar.dataCode.length);
                        get_list_data.push(dar);
                        break;
                    case 1:
                        let value = parseData(subChoiceFrameResp.attr.get(i).result.data);
                        get_list_data.push(value);
                        break;
                }
            }
            get_list.seq_get_list = get_list_data;
            frameData.code = resultCode.success;
            frameData.data = get_list;
            break;
        case 2:
            let get_record_data = new Array<any>();
            let get_record_list = {
                seq_get_record_list: {

                },
            }
            for (let i = 0; i < subChoiceFrameResp.recordAttr.size(); i++) {
                switch (subChoiceFrameResp.recordAttr.get(i).respData.getChoiceIndex()) {
                    case 0:
                        let dar = {
                            dar: {},
                        };
                        subChoiceFrameResp.recordAttr.get(i).respData.dar.encodeAndSave(1000);
                        dar.dar = DataUtils.bytesToHexString(subChoiceFrameResp.recordAttr.get(i).respData.dar.dataCode, 0, subChoiceFrameResp.recordAttr.get(i).respData.dar.dataCode.length);
                        get_record_data.push(dar);
                        break;
                    case 1:
                        let seq_of_a_record = {
                            seq_of_a_record: {},
                        };
                        let a_record = {
                            a_record: {},
                        }
                        let seq_a_record_list = new Array<any>();
                        for (let j = 0; j < subChoiceFrameResp.recordAttr.get(i).respData.records.size(); j++) {
                            let seq_data = {
                                seq_of_data: {}
                            }
                            let seq_data_list = new Array<String>();
                            for (let k = 0; k < subChoiceFrameResp.recordAttr.get(i).respData.records.get(j).data.size(); k++) {
                                let value = parseData(subChoiceFrameResp.recordAttr.get(i).respData.records.get(j).data.get(k));
                                seq_data_list.push(value);
                            }
                            a_record.a_record = seq_data_list
                            seq_data.seq_of_data = seq_data_list;
                            seq_a_record_list.push(a_record);
                        }
                        seq_of_a_record.seq_of_a_record = seq_a_record_list;
                        get_record_data.push(seq_of_a_record);
                        break;
                }
            }
            get_record_list.seq_get_record_list = get_record_data
            frameData.data = get_record_list;
            frameData.code = resultCode.success;
            break;
    }
    return JSON.stringify(frameData);
}
